/*
 * @Author: your name
 * @Date: 2021-10-12 10:49:34
 * @LastEditTime: 2021-10-13 16:45:58
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /a1902-a-group/admin_pc/src/store/index.js
 */
import { createStore } from 'vuex'
import {login} from '../api/home'
import Cookies from "js-cookie";

export default createStore({
  state: {
    user: {}, // 用户信息
    token: Cookies.get('token') || '', // token值
  },
  // mutations 是唯一 可以修改start 的值
  mutations: {
    // 修改用户信息和token的
    getUser(state, payload) {
      state.user = {...payload};
      state.token = payload.token;
      Cookies.set('token', payload.token);
    }
  },
  // vuex 中 我们处理 异步操作的
  actions: {
    // 登陆接口
    // store 就是store
    // payload 就是传递的参数
    async ACTION_LOGIN ({commit}, payload) {
      const data = await login('/login',  {
          username: '123',
          passwrod: '123'
      })
      commit('getUser', data.data);
    }
  },
  modules: {
  }
})
